<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/21
 * Time: 11:01
 */

namespace app\common\model;


use think\Model;
use traits\model\SoftDelete;

class BusInquirySuppliers extends Model
{
    use SoftDelete;
    protected $table = 'bus_inquiry_suppliers_company';
    protected $deleteTime = 'delete_time';
    protected $hidden = ['delete_time'];
    protected $field = true;

    public function getDeadlineDatetimeAttr($name){
        return $name?date('Y-m-d H',strtotime($name)):'';
    }

    public function salesCompany()
    {
        return $this->belongsTo('SalesCompany','bus_from_company_id','id')->bind('province,city,district');
    }

    public function detailInfo()
    {
        return $this->hasMany('BusInquiryDetailSupplier','bus_inquiry_suppliers_id','id')->order('order_no asc,id asc');
    }

    public function detailSales()
    {
        return $this->hasMany('BusInquiryDetailSupplier','bus_inquiry_suppliers_id','id')->where(['state'=>['>',3]])->order('order_no asc,id asc');
    }
    public function detailNum()
    {
        return $this->hasOne('BusInquiryDetailSupplier','bus_inquiry_suppliers_id','id')
            ->field('bus_inquiry_suppliers_id,sum(num) as sum_num')
            ->group('bus_inquiry_suppliers_id')
            ->bind('sum_num');
    }

    public function inquiry()
    {
        return $this->belongsTo('BusInquiry','bus_inquiry_id','id')->field('id,state as inquiry_state')->bind('inquiry_state');
    }
    public function income()
    {
        return $this->belongsTo('BusInquiry','bus_inquiry_id','id')
            ->field('id,totall_price as sales_totall_price,freight as sales_freight,tax_rate as sales_tax_rate,returned_money as sales_returned_money,returned_money_type as sales_returned_money_type,returned_money_datetime as sales_returned_money_datetime,returned_money_state as sales_returned_money_state,invoice_state as sales_invoice_state,invoice_datetime as sales_invoice_datetime,loss_assessor_money,loss_assessor_pay,loss_assessor_type,loss_assessor_state,loss_assessor_datetime,repair_company_state,repair_company_money,repair_company_type,repair_company_pay,repair_company_datetime')
            ->bind('sales_totall_price,sales_freight,sales_tax_rate,sales_returned_money,sales_returned_money_type,sales_returned_money_datetime,sales_returned_money_state,sales_invoice_state,sales_invoice_datetime,loss_assessor_money,loss_assessor_pay,loss_assessor_type,loss_assessor_state,loss_assessor_datetime,repair_company_state,repair_company_money,repair_company_type,repair_company_pay,repair_company_datetime');
    }

}